package com.jiazhong.basic.borrow;

import com.jiazhong.basic.borrow.bean.Book;
import com.jiazhong.basic.borrow.bean.User;
import com.jiazhong.basic.borrow.service.BookService;
import com.jiazhong.basic.borrow.service.BorrowService;
import com.jiazhong.basic.borrow.service.UserService;
import com.jiazhong.basic.borrow.service.impl.BookServiceImpl;
import com.jiazhong.basic.borrow.service.impl.BorrowServiceImpl;
import com.jiazhong.basic.borrow.service.impl.UserServiceImpl;

import java.util.List;
import java.util.Scanner;

public class App {

    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        BookService bookService = new BookServiceImpl();
        BorrowService borrowService = new BorrowServiceImpl();
        System.out.print("请输入账号：");
        String username = scan.next();
        System.out.print("请输入密码：");
        String password = scan.next();
        User user = userService.login(username, password);
        if (user == null) {
            System.out.println("账号或者密码错误");
            return;
        }
        System.out.println("欢迎" + user.getUsername() + "访问");
        System.out.println("请选择操作：");
        System.out.println("1.借阅");
        System.out.println("2.归还");
        int choose = scan.nextInt();
        switch (choose) {
            case 1 -> {
                System.out.println("这个是我们的图书列表:");
                List<Book> books = bookService.find();
                books.forEach(e -> {
                    System.out.println(e.getId() + "\t" + e.getName() + "\t" + e.getAuthor() + "\t" + e.getNumber());
                });
                System.out.println("请选择你要借阅的图书：");
                int bookId = scan.nextInt();
                int row = borrowService.borrow(user.getId(), bookId);
                System.out.println(row > 0 ? "借阅成功" : "借阅失败");
            }
            case 2 -> {
                System.out.println("请选择你要归还的图书：");
                int bookId = scan.nextInt();
                int row = borrowService.back(user.getId(), bookId);
                System.out.println(row > 0 ? "归还成功" : "归还失败");

            }
        }

    }
}
/*
create table book
(
    id int primary key auto_increment,
    name varchar(100) not null,
    author varchar(20),
    number int ,
    `desc` longtext
);
insert into book values(null,'红楼梦','曹雪芹',2,'儿女情长');
insert into book values(null,'西游记','吴承恩',5,'猴子、猪、妖怪和人');
insert into book values(null,'三国演义','罗贯中',6,'三个国家的事情');
insert into book values(null,'水浒传','施耐庵',3,'108个好汉');

create table `user`
(
    id int primary key auto_increment,
    username varchar(20) not null,
    `password` varchar(100) ,
    gender char(1)
);
insert into `user` values(null,'zhangsan',md5('zhangsan'),'男');
insert into `user` values(null,'lisi',md5('lisi'),'女');
insert into `user` values(null,'wangwu',md5('wangwu'),'男');
create table borrow
(
    id int primary key auto_increment,
    user_id int,
    book_id int,
    borrow_date datetime default now(),
    back_date datetime
);
 */